package dao

import (
	"errors"

	"gitee.com/cyanbleeze/go-image-management/internal/model"
	"gitee.com/cyanbleeze/go-image-management/pkg/app"
)

func (d *Dao) CreateComment(content string, uID uint, pID uint) error {
	comment := model.Comment{
		Content:   content,
		UserID:    uID,
		PictureID: pID,
	}
	return comment.Create(d.engine)
}

func (d *Dao) UpdateComment(id uint, content string, uID uint, pID uint) error {
	comment := model.Comment{
		UserID:    uID,
		PictureID: pID,
	}
	comment.ID = id
	_, err := comment.Get(d.engine)
	if err != nil {
		return errors.New("图片分类不存在")
	}
	comment.Content = content
	return comment.Update(d.engine)
}

func (d *Dao) DeleteComment(id uint, userID uint) error {
	comment := model.Comment{
		UserID: userID,
	}
	comment.ID = id
	_, err := comment.Get(d.engine)
	if err != nil {
		return errors.New("图片分类不存在")
	}
	return comment.Delete(d.engine)
}

func (d *Dao) GetComment(id uint, uID uint) (model.Comment, error) {
	comment := model.Comment{
		UserID: uID,
	}
	comment.ID = id
	return comment.Get(d.engine)
}

func (d *Dao) GetCommentListByPictureIDAndUserID(uID uint, pID uint, page int, pageSize int) ([]*model.Comment, error) {
	comment := model.Comment{
		UserID:    uID,
		PictureID: pID,
	}
	return comment.List(d.engine, app.GetPageOffset(page, pageSize), pageSize)
}

func (d *Dao) GetCommentListByPictureID(pID uint, page int, pageSize int) ([]*model.Comment, error) {
	comment := model.Comment{
		PictureID: pID,
	}
	return comment.List(d.engine, app.GetPageOffset(page, pageSize), pageSize)
}

func (d *Dao) CountCommentListByPictureIDAndUserID(uID uint, pID uint) (int, error) {
	comment := model.Comment{
		UserID:    uID,
		PictureID: pID,
	}
	return comment.Count(d.engine)
}

func (d *Dao) CountCommentListByPictureID(pID uint) (int, error) {
	comment := model.Comment{
		PictureID: pID,
	}
	return comment.Count(d.engine)
}
